#!coding=utf-8
'''
Created on 2014-5-23

@author: Administrator
'''
import time
from django.dispatch.dispatcher import receiver
from weixin.signals import wxsignal_subscribe_event,\
    wxsignal_location_event
from weixin.models import User

@receiver(wxsignal_subscribe_event)
def process_subscribe_event(sender,**kwargs):
    tmpopenid=kwargs['msg']['FromUserName']
    userext=User.objects.filter(openid=tmpopenid)
    if userext.exists()==False:
        usertmp=User(openid=tmpopenid,subscribe_time=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
        usertmp.save()
        
@receiver(wxsignal_location_event)
def process_location_event(sender,**kwargs):
    tmpopenid=kwargs['msg']['FromUserName']
    lat=kwargs['msg']['Latitude']
    longi=kwargs['msg']['Longitude']
    preci=kwargs['msg']['Precision']
    userext=User.objects.filter(openid=tmpopenid)
    if userext.exists()==True:
        userext.update(latitude=lat,longitude=longi,precision=preci,modify_at=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
        
        

    